package cn.buk.tms.flight.service;

import cn.buk.tms.dao.BasicDataDao;
import cn.buk.tms.dao.FlightChangeOrderDao;
import cn.buk.tms.dao.FlightOrderDao;
import cn.buk.tms.dao.FlightRefundOrderDao;
import cn.buk.tms.entity.order.flight.FlightChangeOrder;
import cn.buk.tms.entity.order.flight.FlightOrder;
import cn.buk.tms.entity.order.flight.FlightOrderFlight;
import cn.buk.qms.service.BaseAbstractService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderCheckServiceImpl extends BaseAbstractService implements OrderCheckService {

  private static final Logger logger = LogManager.getLogger(OrderCheckServiceImpl.class);

  @Autowired
  private FlightChangeOrderDao changeOrderDao;

  @Autowired
  private FlightRefundOrderDao refundOrderDao;

  @Autowired
  private FlightOrderDao flightOrderDao;

  @Autowired
  protected BasicDataDao basicDataDao;

  @Override
  public void checkChangeOrderMobile(int enterpriseId, String username) {
    var changeOrders = changeOrderDao.searchMobileIsNull(enterpriseId);
    for (var o : changeOrders) {
      if (o.getMobile() != null) {
        continue;
      }
      var flightOrder = flightOrderDao.getOrderDetailByOrderNo(enterpriseId, o.getOrderNo());
      if (flightOrder == null) {
        continue;
      }

      for (var psgInfo : flightOrder.getPassengers()) {
        if (psgInfo.getName().equalsIgnoreCase(o.getPsgName())
                && psgInfo.getIdNo().equalsIgnoreCase(o.getIdNo())) {
          if (psgInfo.getMobile().length() == 11) {
            changeOrderDao.updateChangeOrderMobile(enterpriseId, username, o.getId(), psgInfo.getMobile());
          }

          break;
        }
      }
    }
  }


  @Override
  public void checkRefundOrderMobile(int enterpriseId, String username) {
    var refundOrders = refundOrderDao.searchMobileIsNull(enterpriseId);
    for (var o : refundOrders) {
      if (o.getMobile().length() > 0) {
        continue;
      }
      var flightOrder = flightOrderDao.getOrderDetailByOrderNo(enterpriseId, o.getOrderNo());
      if (flightOrder == null) {
        continue;
      }

      for (var psgInfo : flightOrder.getPassengers()) {
        if (psgInfo.getName().equalsIgnoreCase(o.getPsgName())
                && psgInfo.getIdNo().equalsIgnoreCase(o.getIdNo())) {
          if (psgInfo.getMobile().length() == 11) {
            refundOrderDao.updateRefundOrderMobile(enterpriseId, username, o.getId(), psgInfo.getMobile());
          }

          break;
        }
      }
    }
  }



  @Override
  public void checkFlightOrderAirportName(final int enterpriseId, final String username) {
    List<FlightOrderFlight> orderFlightList = flightOrderDao.searchFlightOrderFlightsAirportNameIsNull(enterpriseId);
    if (!orderFlightList.isEmpty()) {
      logger.info("checkFlightOrderAirportName: {}", orderFlightList.size());
    }
    for (FlightOrderFlight orderFlight : orderFlightList) {
      if (orderFlight.getFlight().getDepartureAirportName() == null) {
        doCheckAirportName(orderFlight.getId(), 0, orderFlight.getFlight().getDepartureAirport());
      }
      if (orderFlight.getFlight().getArrivalAirportName() == null) {
        doCheckAirportName(orderFlight.getId(), 1, orderFlight.getFlight().getArrivalAirport());
      }
    }
  }

  private void doCheckAirportName(final int orderFlightId, final int portType, final String airportCode) {
    final String airportName = getAirportName(airportCode);
    if (StringUtils.trimToEmpty(airportName).length() > 0) {
      flightOrderDao.updateFlightOrderFlightAirportName(orderFlightId, portType, airportName);
    }
  }


  @Override
  public void checkRefundOrderSupplier(final int enterpriseId, String username) {
    var refundOrders = refundOrderDao.searchSupplierIsNull(enterpriseId);
    if (!refundOrders.isEmpty()) {
      logger.info("checkRefundOrderSupplier: {}", refundOrders.size());
    }
    for (var o : refundOrders) {
      if (o.getSupplier() != null || o.getPaymentMethod() != null) {
        continue;
      }

      var flightOrder = flightOrderDao.getOrderDetailByOrderNo(enterpriseId, o.getOrderNo());
      if (flightOrder == null) {
        continue;
      }

      if (flightOrder.getSupplier() == null || flightOrder.getPaymentMethod() == null) {
        continue;
      }

      int retCode = refundOrderDao.updateRefundOrderSupplier(enterpriseId, username, o.getId(), flightOrder.getSupplier(), flightOrder.getPaymentMethod());
      if (retCode > 0) {
        String content = username + " 更新退票单(" + o.getRefundOrderNo() + ")供应商为 "
                + flightOrder.getSupplier().getName() + ", " +
                "支出方式为 " + flightOrder.getPaymentMethod().getName();
        doSendAlert(enterpriseId, content, "Dai", 0);
      }

    }
  }



  @Override
  public void checkChangeOrderSupplier(int enterpriseId, String username) {
    List<FlightChangeOrder> changeOrders = changeOrderDao.searchSupplierIsNull(enterpriseId);
    if (!changeOrders.isEmpty()) {
      logger.info("checkChangeOrderSupplier: {}", changeOrders.size());
    }
    for (FlightChangeOrder o : changeOrders) {
      if (o.getSupplier() != null || o.getPaymentMethod() != null) {
        continue;
      }

      FlightOrder flightOrder = flightOrderDao.getOrderDetailByOrderNo(enterpriseId, o.getOrderNo());
      if (flightOrder == null) {
        continue;
      }

      if (flightOrder.getSupplier() == null || flightOrder.getPaymentMethod() == null) {
        continue;
      }

      int retCode = changeOrderDao.updateChangeOrderSupplier(enterpriseId, username, o.getId(), flightOrder.getSupplier(), flightOrder.getPaymentMethod());
      if (retCode > 0) {
        String content = username + " 更新改签单(" + o.getChangeOrderNo() + ")供应商为 "
                + flightOrder.getSupplier().getName() + ", " +
                "支出方式为 " + flightOrder.getPaymentMethod().getName();
        doSendAlert(enterpriseId, content, "Dai", 0);
      }

    }
  }

}
